package top.maof.book.spider.develop.contents.as;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import top.maof.book.model.Book;
import top.maof.book.model.Contents;
import top.maof.book.spider.develop.Split;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.model.AfterExtractor;
import us.codecraft.webmagic.model.annotation.ExtractBy;
import us.codecraft.webmagic.model.annotation.HelpUrl;
import us.codecraft.webmagic.model.annotation.TargetUrl;

@Data
@TargetUrl("https://www.23xstxt.com/book/\\d+/\\d+/")
@HelpUrl(value = {"https://www.23xstxt.com/", "https://www.23xstxt.com/class/1_1.html",
        "https://www.23xstxt.com/book/\\d+/\\d+/",
        "https://www.23xstxt.com/class/2_1.html", "https://www.23xstxt.com/class/3_1.html",
        "https://www.23xstxt.com/class/4_1.html", "https://www.23xstxt.com/class/5_1.html",
        "https://www.23xstxt.com/class/6_1.html", "https://www.23xstxt.com/class/7_1.html",
        "https://www.23xstxt.com/class/8_1.html", "https://www.23xstxt.com/class/9_1.html",
        "https://www.23xstxt.com/class/10_1.html", "https://www.23xstxt.com/full.html"
})
@Slf4j
public class X23 implements AfterExtractor, Split {

    @ExtractBy(value = "//div[@id=info]/h1/text()")
    private String name;

    @ExtractBy(value = "//div[@id=info]/p[1]/text()")
    private String author;//作者:伊然淳静

    private String url;

    private String content;

    private Integer webId = 16;

    public static final String CHARSET = "gbk";

    @Override
    public Book getBook() {
        Book book = new Book();
        book.setAuthor(this.author);
        book.setName(this.name);
        return book;
    }

    @Override
    public Contents getContents() {
        Contents contents = new Contents();
        contents.setUrl(this.url);
        contents.setContent(this.content);
        contents.setWebId(this.webId);
        return contents;
    }


    @Override
    public void afterProcess(Page page) {

        //对得到的作者字段做处理  作者：唐家三少
        this.author = this.author.substring(this.author.indexOf("：") + 1);

        log.info(this.name + "-" + this.author);


        //获取该页面url
        String url = page.getRequest().getUrl();
        this.setUrl(url);
    }
}
